package com.ppp.poi.bean.base;

import com.ppp.poi.bean.enumeration.RES_STATUS;
import com.ppp.poi.common.exception.AppException;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Wu Jin
 * @create 2020-01-19
 */
@Data
@NoArgsConstructor
public class AppResult<T> {

    /**
     * 对外返回的对象
     */
    private T data;

    /**
     * 返回状态码
     */
    private int code = RES_STATUS.SERVER_UNKONW_ERROR.code;

    /**
     * 返回消息
     */
    private String msg = RES_STATUS.SERVER_UNKONW_ERROR.msg;

    public AppResult(RES_STATUS status) {
      super();
      this.code = status.code;
      this.msg = status.msg;
    }

    public AppResult(int code, String msg) {
      this.code = code;
      this.msg = msg;
    }

    public AppResult(T data) {
      this.data = data;
      this.code = RES_STATUS.SUCCESS.code;
      this.msg = RES_STATUS.SUCCESS.msg;
    }

    public AppResult(T data, int code, String msg) {
      this.data = data;
      this.code = code;
      this.msg = msg;
    }

  public static AppResult ok() {
    return new AppResult(RES_STATUS.SUCCESS);
  }

  public void setStatus(RES_STATUS status) {
      this.code = status.code;
      this.msg = status.msg;
    }

    public void setStatus(AppException e) {
      this.code = e.getCode();
      this.msg = e.getMsg();
    }


    /**
     * 服务器unix utc时间戳秒值
     */
    public long getTimestamp() {
      return System.currentTimeMillis() / 1000;
    }

}
